﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.PCClient.Helper;
using MahApps.Metro.Controls;
using System;
using System.Windows;
using System.Windows.Input;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for UserValidateWnd.xaml
    /// </summary>
    public partial class UserValidateWnd : MetroWindow
    {
        public UserValidateWnd()
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
            this.txtEmployeeCode.Focus();
        }
        //UserAuthenticate userAuth = new UserAuthenticate();
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string strUser = this.txtEmployeeCode.Text.ToString().Trim();
            string strPassword = this.txtPassword.Password.ToString().Trim();
            if (string.IsNullOrEmpty(strUser))
            {
                this.txtEmployeeCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(strPassword))
            {
                this.txtPassword.Focus();
                return;
            }
            string msg;
            bool bRet = ACC.Extensions.PCClient.BaseWindow.BaseCustomWindow_New.accProcess.GetUserCheck(strUser, strPassword, out msg);
            //AccResult accRes = userAuth.UserValidate(strUser, strPassword);
            //if (accRes.Result)
            //{
            //    DialogResult = true;
            //    this.Close();
            //}
            if(bRet)
            {
                DialogResult = true;
                this.Close();
            }
            else
            {
                MessageDialogWnd.ShowDialog(string.Format(LanguageHelper.LoadStringFromResource("MSG_WARNING")),
                msg //string.Format(LanguageHelper.LoadStringFromResource("MSG_USER_FAIL"), strUser)
                );
                DialogResult = false;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }

        private void MetroWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }
            btnLogin_Click(null, null);
        }

        private void MetroWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

    }
}
